package Network;

import java.util.ArrayList;
import java.util.HashMap;

import BlindCounters.DoubleBlindCounter;
import DataStructures.PokerTable;
import PokerComponents.HoldEmDealer;

public class NetworkHoldemDealer extends HoldEmDealer implements DealerMonitor {

	private HashMap<Integer, RemotePokerPlayer> observers;
	
	public NetworkHoldemDealer(PokerTable table, DoubleBlindCounter timer, int turnTime, int stackSize) {
		super(table, timer, turnTime, stackSize);
		observers = new HashMap<Integer, RemotePokerPlayer>();
	}

	private synchronized boolean isValidToPerformAction(int seatNumber){
		if(isGameOver()){
			return false;
		}
		if(seatNumber != pIter.getActivePlayer().getSeatNumber()){
			return false;
		}
		notifyAll();
		return true;
	}
	
	public void addObservingPlayer(RemotePokerPlayer rpp){
		observers.put(new Integer(rpp.getPlayerId()), rpp);
	}
	
	@Override
	public synchronized void seatRequest(/*Some type of stream goes here */){
		// check if any seats are left
		
		// if a seat is free create a RemotePokerPlayer 
		
		// send them their seat number
		
		// else make them an observer (functionality prob wont be added on first release
	}
	
	@Override
	public synchronized void raise(int seatNumber, int amount) {
		if(isValidToPerformAction(seatNumber)){
			table.placeChipsInFrontOfPlayer(seatNumber, amount);
			table.getPlayer(seatNumber).removeChips(amount);
			announce.announceRaise(pIter.getActivePlayer().getPlayerId(), amount);
			pIter.gotoNextPlayer();
		}
		notifyAll();
	}
	
	@Override
	public synchronized void check(int seatNumber) {
		if(isValidToPerformAction(seatNumber)){
			announce.announceCheck(pIter.getActivePlayer().getPlayerId());
			pIter.gotoNextPlayer();			
		}
		notifyAll();
	}

	@Override
	public synchronized void fold(int seatNumber) {
		if(isValidToPerformAction(seatNumber)){
			announce.announceFold(pIter.getActivePlayer().getPlayerId());
			pIter.removeAndGotoPlayer();
		}
		notifyAll();
	}
	
	
	@Override
	public synchronized int getNewPlayerID() {
		//TODO: check we dont assign an ID a second time!
		return (int) (Math.random()*1000000000);
	}
}
